import { ButtonPushAfterEvent, Player } from "@minecraft/server";
import { Game, GameComponent } from "@sapi-game/main";
import { DimensionIds, EntityTypeIds } from "@sapi-game/utils/vanila-data";
import { backHub } from "@/base/func";
import { isAdmin } from "sapi-pro/scripts/func";
import { eleBackState } from "./state";

export class EleBackStateButtons extends GameComponent<eleBackState> {
    override onAttach(): void {
        //开始按钮
        this.bind([371, -58, 149], (e) => {
            const p = e.source as Player;
            if (!isAdmin(p)) return p.sendMessage("你不是管理员，无法开始");
            this.state.startGame();
        });
        //返回按钮
        this.bind([369, -58, 161], (e) => {
            const p = e.source as Player;
            backHub(p);
        });
    }

    bind(
        loc: [number, number, number],
        func: (e: ButtonPushAfterEvent) => void
    ) {
        this.subscribe(Game.events.buttonPush, func, {
            loc: loc,
            dimensionId: DimensionIds.Overworld,
            sourceType: EntityTypeIds.Player,
        });
    }
}
